package com.mes.cloud.service;

import com.mes.cloud.api.dto.request.MergeQueryRequest;
import com.mes.cloud.api.dto.request.employee.SysEmployeePositionRequest;
import com.mes.cloud.api.dto.request.organization.BaseChannelCustomerRequest;
import com.mes.cloud.api.dto.response.MergeQueryDto;
import com.mes.cloud.api.dto.response.SysParamDto;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统参数表
 *
 * @author Mr.zhang
 * @version 2019-08-02 10:37:18
 * @email zhangchao4@shinho.net.cn
 */
@Service
public class MergeQueryService {

    private SysPositionOrganizationAuthService sysPositionOrganizationAuthService;
    private SysOrganizationService sysOrganizationService;
    private SysParamService sysParamService;
    private BaseChannelService baseChannelService;
    private SysEmployeePositionService sysEmployeePositionService;


    @Autowired
    public MergeQueryService(SysPositionOrganizationAuthService sysPositionOrganizationAuthService
            , SysOrganizationService sysOrganizationService
            , SysParamService sysParamService
            , BaseChannelService baseChannelService
            , SysEmployeePositionService sysEmployeePositionService) {
        this.sysPositionOrganizationAuthService = sysPositionOrganizationAuthService;
        this.sysOrganizationService = sysOrganizationService;
        this.sysParamService = sysParamService;
        this.baseChannelService = baseChannelService;
        this.sysEmployeePositionService = sysEmployeePositionService;
    }

    public JsonResult<MergeQueryDto> organizationMergeQuery(MergeQueryRequest mergeQueryRequest) {
        MergeQueryDto mergeQueryDto = new MergeQueryDto();
        JsonResult<List<String>> listJsonResult = sysPositionOrganizationAuthService.getOrganizationIdList();
        List<String> orgAuthIds = listJsonResult.getData();
        //通过授权组织id 查询授权组织id所有下级
        if (!CollectionUtils.isEmpty(orgAuthIds) && orgAuthIds.size() > 0) {
            JsonResult<List<String>> orgListJsonResult = sysOrganizationService.listChildren(orgAuthIds);
            //如果当前登录者存在授权组织
            mergeQueryDto.setOrganizationIds(orgListJsonResult.getData());

        }
        //如果组织id不为空 继续查询系统参数、渠道
        if (!CollectionUtils.isEmpty(mergeQueryDto.getOrganizationIds())) {
            //查询系统参数 获取特殊权限配置
            JsonResult<SysParamDto> sysParamJsonResult = sysParamService.getSysParam(Constants.ALL_CUSTOMER_ORG);
            SysParamDto sysParamDto = sysParamJsonResult.getData();
            String value = sysParamDto.getValue();
            mergeQueryDto.setValue(value);

            this.queryChannelIds(mergeQueryRequest, mergeQueryDto);

        }

        JsonResult<MergeQueryDto> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(mergeQueryDto);
        return jsonResult;
    }


    public JsonResult<MergeQueryDto> positionMergeQuery(MergeQueryRequest mergeQueryRequest) {
        MergeQueryDto mergeQueryDto = new MergeQueryDto();

        //查询当前登录员工的岗位
        SysEmployeePositionRequest sysEmployeePositionRequest = new SysEmployeePositionRequest();
        sysEmployeePositionRequest.setEmployeeId(UserUtil.getEmployeeId());
        JsonResult<List<String>> positionListJsonResult = sysEmployeePositionService.queryPositionIdList(sysEmployeePositionRequest);
        mergeQueryDto.setPositionIds(positionListJsonResult.getData());

        //如果组织id不为空 继续查询系统参数、渠道
        if (!CollectionUtils.isEmpty(mergeQueryDto.getPositionIds())) {

            this.queryChannelIds(mergeQueryRequest, mergeQueryDto);
        }

        JsonResult<MergeQueryDto> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(mergeQueryDto);
        return jsonResult;
    }

    private void queryChannelIds(MergeQueryRequest mergeQueryRequest, MergeQueryDto mergeQueryDto) {
        if (!StringUtils.isEmpty(mergeQueryRequest.getChannelId())) {
            BaseChannelCustomerRequest baseChannelCustomerRequest = new BaseChannelCustomerRequest();
            //设置渠道id
            baseChannelCustomerRequest.setChannelId(mergeQueryRequest.getChannelId());
            //设置渠道属性
//            String channelAttribute = mergeQueryRequest.getChannelAttribute();
//            baseChannelCustomerRequest.setChannelAttribute(channelAttribute);

            List<String> channelIds = new ArrayList<>();
            channelIds.add(mergeQueryRequest.getChannelId());
            //查询当前渠道的所有下级渠道
            JsonResult<List<String>> channelIdJsonResult = baseChannelService.findBaseChannelList(baseChannelCustomerRequest);
            channelIds.addAll(channelIdJsonResult.getData());
            mergeQueryDto.setChannelIds(channelIds);
        }
    }
}
